﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(RelativeMove))]
public class RelativeMoveEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var o = (RelativeMove)target;
        base.OnInspectorGUI();
        if (GUILayout.Button("SetOrigin"))
        {
            o.SetOrigin();
        }
    }
}
#endif
public class RelativeMove : MonoBehaviour
{
    public Tran3 originLocalTran3;
    public Matrix4x4 originLocalMatrix;
    public Vector3 relativePos
    {
        set
        {
            _relativePos = value;
            var pos = (transform.parent.localToWorldMatrix * originLocalMatrix).MultiplyPoint3x4(_relativePos);
            if (mirrorOn && mirror != null)
            {
                mirror.position = pos.SetX(-pos.x);
            }
            transform.position = pos;
        }
        get { return _relativePos; }
    }
    private Vector3 _relativePos;
    public Transform mirror;
    public bool mirrorOn = true;
    public void Reset()
    {
        SetOrigin();
    }
    public void SetOrigin()
    {
        originLocalTran3 = transform.LocalTran3();
        originLocalMatrix = originLocalTran3;
    }
}
